package com.qfedu.dao.impl;

import com.qfedu.dao.MilkTeaMenuDao;
import com.qfedu.model.MilkTea;
import com.qfedu.utils.DruidUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class MilkTeaMeauDaoImpl implements MilkTeaMenuDao {
        JdbcTemplate jdbcTemplate=new JdbcTemplate(DruidUtil.getDataSource());
    @Override
    public List<MilkTea> findMilkTea(int pageNo, int pageSize) {
        String sql="select tid,tname,price,genre,img_path from tea limit ?,?";
        List<MilkTea> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(MilkTea.class), (pageNo - 1) * pageSize, pageSize);
        return query;
    }
    //查询所有条数
    @Override
    public int count() {
        String sql="select count(*) from tea";
        Integer integer = jdbcTemplate.queryForObject(sql, int.class);
        return integer;
    }
    //id查找
    @Override
    public MilkTea findMilkTeaById(int byId) {
        String sql="select * from tea where tid=?";
        List<MilkTea> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(MilkTea.class), byId);
        return query.get(0);
    }
    //添加
    @Override
    public int add(MilkTea milkTea) {
        String sql="insert into tea values(null,?,?,?,?)";
        int update = jdbcTemplate.update(sql, milkTea.getTname(), milkTea.getPrice(), milkTea.getGenre(), milkTea.getImg_path());
        return update;
    }

    //删除
    @Override
    public int delete(int tid){
        String sql="delete from tea where tid=?";
        int update = jdbcTemplate.update(sql, tid);
        return update;
    }
    //修改
    @Override
    public int update(MilkTea milkTea) {

        String sql="update tea set tname=?,price=?,genre=?,img_path=? where tid=?";
        int update = jdbcTemplate.update(sql,milkTea.getTname(),milkTea.getPrice(),milkTea.getGenre(),milkTea.getImg_path(),milkTea.getTid());
        return update;
    }
    //查询所有
    @Override
    public List<MilkTea> findMilkTeaAll() {
        String sql="select * from tea";
        List<MilkTea> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(MilkTea.class));
        return query;
    }
}
